﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;

namespace Karasoft.Web.UI.Module
{
    public class FriendlyErrorsModule : IHttpModule
    {

        public void Init(HttpApplication app)
        {

            app.Error += new EventHandler(app_Error);

        }

        /// <summary> 

        /// When debug mode is enabled, display 

        /// friendly error page 

        /// </summary> 

        void app_Error(object sender, EventArgs e)
        {

            HttpApplication app = (HttpApplication)sender;

            HttpContext context = app.Context;

            Exception error = context.Server.GetLastError().GetBaseException();

            context.Response.Clear();

            CompilationSection compilationConfig = (CompilationSection)WebConfigurationManager.GetWebApplicationSection("system.web/compilation");

            if (compilationConfig.Debug)

                context.Server.Transfer("~/Error.aspx");

            else

                context.Server.Transfer("~/Error.aspx");

        }

        public void Dispose()
        {

        }

    }

}
